/**
 * 
 */
package dms.presentation.action.supervisor;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.actions.MappingDispatchAction;
import org.dom4j.Document;
import org.dom4j.Element;

import dms.dto.BulletinDTO;
import dms.dto.LowElectricityDTO;
import dms.dto.MailDTO;
import dms.dto.NoticeDTO;
import dms.model.Building;
import dms.model.Dormitory;
import dms.model.form.Bulletin;
import dms.model.form.LowElectricity;
import dms.model.form.Mail;
import dms.model.form.Notice;
import dms.model.role.DormSupervisor;
import dms.model.role.wrapper.DormSupervisorWrapper;
import dms.model.util.PageIterator;
import dms.service.BulletinQueryService;
import dms.service.DormitoryService;
import dms.service.LowElectricityQueryService;
import dms.service.MailQueryService;
import dms.service.NoticeQueryService;
import dms.util.Constant;
import dms.util.ContextUtil;
import dms.util.DateFormatUtil;
import dms.util.PageUtil;
import dms.util.XMLUtil;

/**
 * @author 李健
 *
 */
public class BuildingWorkAction extends MappingDispatchAction {
	
	private static final Log log = LogFactory.getLog(BuildingWorkAction.class);
	
	public ActionForward bulletinList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		BulletinQueryService bulletinQueryService = (BulletinQueryService) ContextUtil.getContext().getBean("bulletinQueryService");
		BulletinDTO bulletinDTO = (BulletinDTO) form;
		Integer start = null;
		Integer count = null;
		int[] params = null;
		Date fromDate = null;
		Date toDate = null;
		String query = null;
		
		Building building = dormSupervisorWrapper.getDormSupervisor().getBuilding();
		PageIterator<Bulletin> bulletins = null;
		try {
			fromDate = DateFormatUtil.parseDate(bulletinDTO.getFromDate());
			toDate = DateFormatUtil.parseDate(bulletinDTO.getToDate());
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		try {
			query = bulletinDTO.getQuery();
			params = PageUtil.parsePageParameter(request);
			
			start = params[0];
			count = params[1];
			
			if (query != null && query.length() != 0) {
				bulletins = bulletinQueryService.findByBuilding(start, count, fromDate, toDate, building, query);
			} else {
				bulletins = bulletinQueryService.findByBuilding(start, count, fromDate, toDate, building, null);
			}
			request.setAttribute("bulletins", bulletins);
			request.setAttribute("dormSupervisor", dormSupervisorWrapper.getDormSupervisor());
		} catch (Exception e) {
			log.error(e.getMessage());
			return mapping.findForward(Constant.FAILURE);
			
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward addBulletinForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		DormSupervisor dormSupervisor = wrapper.getDormSupervisor();
		request.setAttribute("dormSupervisor", dormSupervisor);

		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward addBulletin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		BulletinDTO dto = (BulletinDTO) form;
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		
		try {
		
			dormSupervisorWrapper.addBulletin(dto);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new Exception("增加公告失败，原因：缺少某些数据");
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward updateBulletin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		BulletinDTO bulletinDTO = (BulletinDTO) form;
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		
		/*//如果有错误跳回
		ActionErrors errors = bulletinDTO.validate(mapping, request);
		if (errors != null || !errors.isEmpty()) {
			response.sendRedirect("updateBulletin.do?id=" + bulletinDTO.getId());
			return null;
		}*/
		
		try {
			dormSupervisorWrapper.updateBulletin(bulletinDTO);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new Exception("无法更新公告");
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward updateBulletinForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		Long bulletinId = null;
		Bulletin bulletin = null;
		BulletinQueryService bulletinQueryService = (BulletinQueryService) ContextUtil.getContext().getBean("bulletinQueryService");
		try {
			bulletinId = Long.valueOf(request.getParameter("id"));
			bulletin = bulletinQueryService.findById(bulletinId);
			request.setAttribute("bulletin", bulletin);
		} catch (Exception e) {
			log.error(e.getMessage());
			e.printStackTrace();
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward deleteBulletin(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		Long bulletinId = null;
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		Document document = XMLUtil.createEncodingDocument("UTF-8");
		Element bulletinElement = document.addElement("response").addElement("bulletin");
		
		try {
			bulletinId = Long.valueOf(request.getParameter("id"));
			dormSupervisorWrapper.deleteBulletinById(bulletinId);
			bulletinElement.addElement("status").addText("true");
			bulletinElement.addElement("message").addText("公告ID：" + bulletinId + "删除成功!");
		} catch (NumberFormatException e) {
			log.error(e.getMessage());
			bulletinElement.addElement("status").addText("false");
			bulletinElement.addElement("message").addText(e.getMessage());
		} catch (Exception e) {			
			log.error(e.getMessage());
			bulletinElement.addElement("status").addText("false");
			bulletinElement.addElement("message").addText(e.getMessage());
		}
		
		response.setContentType("text/xml;charset=UTF-8");
		response.getWriter().print(bulletinElement.asXML());
//		System.out.println(bulletinElement.asXML());
		return null;
	}

	
	public ActionForward lostNoticeList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		NoticeQueryService noticeQueryService = (NoticeQueryService) ContextUtil.getContext().getBean("noticeQueryService");
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		NoticeDTO noticeDTO = (NoticeDTO) form;
		int[] params = null;
		int start = 0;
		int count = 0;
		Date fromDate = null;
		Date toDate = null;
		String query = null;
		PageIterator<Notice> lostNotices = null;
		Building building = wrapper.getDormSupervisor().getBuilding();
		try {
			fromDate = DateFormatUtil.parseDate(noticeDTO.getFromDate());
			toDate = DateFormatUtil.parseDate(noticeDTO.getToDate());
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		try {
			query = noticeDTO.getQuery();
			params = PageUtil.parsePageParameter(request);
			start = params[0];
			count = params[1];
			
			if (query != null && query.length() != 0) {
				lostNotices = noticeQueryService.findLostNoticeByBuilding(start, count, building, query, fromDate, toDate, null);
			} else { 
				lostNotices = noticeQueryService.findLostNoticeByBuilding(start, count, building, null, fromDate, toDate, null);	
			}
			request.setAttribute("lostNotices", lostNotices);
			request.setAttribute("dormSupervisor", wrapper.getDormSupervisor());
		} catch (Exception e) {
			log.error(e.getMessage());
			throw e;
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward foundNoticeList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		NoticeDTO noticeDTO = (NoticeDTO) form;
		int[] params = null;
		int start = 0;
		int count = 0;
		Date fromDate = null;
		Date toDate = null;
		String query = null;
		PageIterator<Notice> foundNotices = null;
		NoticeQueryService noticeQueryService = (NoticeQueryService) ContextUtil.getContext().getBean("noticeQueryService");
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		Building building = wrapper.getDormSupervisor().getBuilding();
		try {
			fromDate = DateFormatUtil.parseDate(noticeDTO.getFromDate());
			toDate = DateFormatUtil.parseDate(noticeDTO.getToDate());
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		try {
			query = noticeDTO.getQuery();
			params = PageUtil.parsePageParameter(request);
			start = params[0];
			count = params[1];
			if (query != null && query.length() != 0) {
				foundNotices = noticeQueryService.findFoundNoticeByBuilding(
						start, count, building, query, fromDate, toDate, null);
			} else {
				foundNotices = noticeQueryService.findFoundNoticeByBuilding(start, count, building, null, fromDate, toDate, null);
			}
			request.setAttribute("foundNotices", foundNotices);
			request.setAttribute("dormSupervisor", wrapper.getDormSupervisor());
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new Exception(e.getMessage());
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward addLostNoticeForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		request.setAttribute("dormSupervisor", getCurrentDormSupervisor());
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward addFoundNoticeForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.setAttribute("dormSupervisor", getCurrentDormSupervisor());
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward addFoundNotice(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		NoticeDTO notice = (NoticeDTO) form;
		
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		try {
			dormSupervisorWrapper.addFoundNotice(notice);
		} catch (Exception e) {
			throw e;
			//throw new Exception(e.getMessage());
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward addLostNotice(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		NoticeDTO notice = (NoticeDTO) form;
		
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		try {
			dormSupervisorWrapper.addLostNotice(notice);
		} catch (Exception e) {
			throw new Exception(e.getMessage());
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward deleteNotice(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Long noticeId = null;
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		Map<String, Object> noticeMap = new HashMap<String, Object>();
		String message = null;
		Boolean status;
		try {
			noticeId = Long.valueOf(request.getParameter("id"));
			wrapper.deleteNoticeById(noticeId);		
			message = "删除成功!";
			status = true;
			noticeMap.put("status", status);
			noticeMap.put("message", "删除成功！");

		} catch (Exception e) {
			e.printStackTrace();
			noticeMap.put("status", false);
			noticeMap.put("message", "删除失败！" + e.getMessage());
		}
		
		JSONObject noticeJSON = JSONObject.fromObject(noticeMap);
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().print(noticeJSON.toString());
		return null;
	}
	
/*	public ActionForward updateNoticeForm(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		Long noticeId = null;
		NoticeQueryService noticeQueryService = (NoticeQueryService) ContextUtil.getContext().getBean("noticeQueryService");
		Notice notice = null;
		Document document = null;
		try {
			noticeId = Long.valueOf(request.getParameter("id"));
			notice = noticeQueryService.findNoticeById(noticeId);
			request.setAttribute("notice", notice);
			document = createNoticeXML(true, "已获取启示ID:" + noticeId + "的信息");
			
		} catch (Exception e) {
			document = createNoticeXML(false, e.getMessage());
		}
		
		response.setContentType("text/xml;charset=UTF-8");
		response.getWriter().print(document.asXML());
		return null;
	}*/
	
	public ActionForward updateNotice(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		NoticeDTO noticeDTO = (NoticeDTO) form;
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		Map<String, Object> noticeMap = new HashMap<String, Object>();
		try {
			wrapper.updateNotice(noticeDTO);
			noticeMap.put("status", true);
			noticeMap.put("message", "更新成功！");
		} catch (Exception e) {
			noticeMap.put("status", false);
			noticeMap.put("message", "更新失败,原因可能是:！" + e.getMessage());
		}
		JSONObject noticeJSON = JSONObject.fromObject(noticeMap);
		
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().print(noticeJSON.toString());
		return null;
	}
	
	public ActionForward addMailForm(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		request.setAttribute("dormSupervisor", getCurrentDormSupervisor());
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward addMail(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		ActionErrors errors = new ActionErrors();
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		MailDTO dto = (MailDTO) form;
		try {
			wrapper.addMail(dto);

		} catch (Exception e) {
			e.printStackTrace();
			errors.add("errors", new ActionMessage(e.getMessage(), false));
			return mapping.getInputForward();
		}
		
		return mapping.findForward("success");
	}
	
	public ActionForward updateMail(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		MailDTO dto = (MailDTO) form;
		JSONObject mailJSON = null;
		Map<String, Object> mailMap = new HashMap<String, Object>();
		try {
			wrapper.updateMail(dto);
			mailJSON = createJSONObject(true, "更新成功!");
		} catch (Exception e) {
			e.printStackTrace();
			mailJSON = createJSONObject(false, "更行失败！原因可能是:" + e.getMessage());
		}
		
		response.setContentType("application/json;charset=UTF-8");
		log.info(mailJSON.toString());
		response.getWriter().print(mailJSON.toString());
		
		return null;
	}
	
	public ActionForward mailList(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		MailQueryService mailQueryService = (MailQueryService) ContextUtil.getContext().getBean("mailQueryService");
		DormitoryService dormitoryService = (DormitoryService) ContextUtil.getContext().getBean("dormitoryService");
		MailDTO dto = (MailDTO) form;
		Date fromDate = null;
		Date toDate = null;
		Building building = this.getCurrentDormSupervisor().getBuilding();
		Dormitory dormitory = null;
		String dormitoryName = null;
		int[] params = null;
		PageIterator<Mail> mails = null;
		try {
			fromDate = DateFormatUtil.parseDate(dto.getFromDate());
			toDate = DateFormatUtil.parseDate(dto.getToDate());
		} catch (Exception e) {
			e.printStackTrace();
			log.warn("MailList中日期解析错误!");
		}
		
		try {						
			params = PageUtil.parsePageParameter(request);
			//类别按key=class;value=dormitory
			String cls = request.getParameter("type");
			if (cls != null && cls.equalsIgnoreCase("dormitory")) {
				dormitoryName = dto.getDormitory();
				dormitory = dormitoryService.findByBuildingAndName(building, dormitoryName);
				mails = mailQueryService.findByDormitory(params[0], params[1], dormitory, fromDate, toDate);
			} else if (cls != null && cls.equalsIgnoreCase("building")){
				mails = mailQueryService.findByBuilding(params[0], params[1], building, fromDate, toDate);
			} else {
				mails = mailQueryService.findByBuilding(params[0], params[1], building, null, null);
			}
			
			request.setAttribute("mails", mails);
			request.setAttribute("dormSupervisor", this.getCurrentDormSupervisor());
			
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward(Constant.FAILURE);
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward deleteMails(ActionMapping mapping,
			ActionForm form, HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DormSupervisorWrapper wrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		Long mailId = null;
		JSONObject mailJSON = null;
		Map<String, Object> mailMap = new HashMap<String, Object>();
		try {
			String[] strIds = request.getParameterValues("mailIds");
			List<Long> ids = new ArrayList<Long>();
			for (String str : strIds) {
				ids.add(Long.valueOf(str));
			}
			wrapper.deleteMailes(ids);
			mailMap.put("status", true);
			mailMap.put("message", "删除成功！");
			
		} catch (Exception e) {
			e.printStackTrace();
			mailMap.put("status", false);
			mailMap.put("message", "删除失败，原因可能是:" + e.getMessage());
			
		}
		mailJSON = JSONObject.fromObject(mailMap);
		response.setContentType("application/json;charset=UTF-8");
		log.info(mailJSON.toString());
		response.getWriter().print(mailJSON.toString());
		
		return null;
	}
	
	public ActionForward addLowElectricityForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		request.setAttribute("dormSupervisor", this.getCurrentDormSupervisor());
		return mapping.findForward(Constant.SUCCESS);
	}
	public ActionForward addLowElectricity(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LowElectricityDTO dto = (LowElectricityDTO) form;
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		
		try {
			dormSupervisorWrapper.addLowElectricity(dto);
		} catch (Exception e) {
			e.printStackTrace();
			
			ActionErrors errors = new ActionErrors();
			errors.add("errors", new ActionMessage("添加失败，原因可能是：" + e.getMessage(), false));
			this.saveErrors(request, errors);
			return mapping.getInputForward();
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	public ActionForward updateLowElectricity(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LowElectricityDTO lowElectricityDTO = (LowElectricityDTO) form;
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		JSONObject jsonObject = null;
		try {
			dormSupervisorWrapper.updateLowElectricity(lowElectricityDTO);
			jsonObject = this.createJSONObject(true, "更新成功...");
		} catch (Exception e) {
			e.printStackTrace();
			log.error(e.getMessage());
			jsonObject = this.createJSONObject(false, e.getMessage());
		}
		
		response.setContentType("application/json;charset=UTF-8");
		response.getWriter().print(jsonObject.toString());
		log.info(jsonObject.toString());
		return null;
	}
	
	public ActionForward deleteLowElectricity(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
			String[] lowElectricityIds = null;
			List<Long> ids = null;
			DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
			JSONObject jsonObject = null;
			Map<String, Object> jsonMap = new HashMap<String, Object>();
			try {
				lowElectricityIds = request.getParameterValues("id");
				ids = new ArrayList<Long>();
				for(String id : lowElectricityIds) {
					ids.add(Long.valueOf(id));
				}
			} catch (Exception e) {
				log.error("id不正确", e);
			}
			
			try {
				dormSupervisorWrapper.deleteLowElectricity(ids);
				jsonObject = this.createJSONObject(true, "删除成功");
			} catch (Exception e) {
				e.printStackTrace();
				log.error("删除失败：" + e.getMessage());
				jsonObject = this.createJSONObject(false, e.getMessage());
			}
			
			response.setContentType("application/json;charset=UTF-8");
			log.info(jsonObject.toString());
			response.getWriter().print(jsonObject.toString());
			return null;
	}
	
	public ActionForward lowElectricityList(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LowElectricityDTO dto = (LowElectricityDTO) form;
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		LowElectricityQueryService lowElectricityQueryService = (LowElectricityQueryService) ContextUtil.getContext().getBean("lowElectricityQueryService");
		DormitoryService dormitoryService = (DormitoryService) ContextUtil.getContext().getBean("dormitoryService");
		int[] params = null;
		Integer start = null;
		Integer count = null;
		PageIterator<LowElectricity> lowELectricityList = null;
		String type = null;
		Date fromDate = null;
		Date toDate = null;
		Building building = this.getCurrentDormSupervisor().getBuilding();
		
		try {
			fromDate = DateFormatUtil.parseDate(dto.getFromDate());
			toDate = DateFormatUtil.parseDate(dto.getToDate());
		} catch (Exception e) {
			log.error("日期解析错误，使用默认值null");
		}
		
		try {
			params = PageUtil.parsePageParameter(request);
			start = params[0];
			count = params[1];
			
			type = dto.getType();
			if (type != null && type.length() != 0) {
				if (type.equalsIgnoreCase("building")) {
					lowELectricityList = lowElectricityQueryService.findByBuilding(start, count, building, fromDate, toDate);
				} else if (type.equalsIgnoreCase("dormitory")) {
					String dormitoryName = dto.getDormitory();
					Dormitory dormitory = dormitoryService.findByBuildingAndName(building, dormitoryName);
					lowELectricityList = lowElectricityQueryService.findByDormitory(start, count, dormitory, fromDate, toDate);
				}
			} else {
				lowELectricityList = lowElectricityQueryService.findByBuilding(start, count, building, null, null);
			}
			request.setAttribute("lowElectricities", lowELectricityList);
			request.setAttribute("dormSupervisor", this.getCurrentDormSupervisor());
		} catch (Exception e) {
			e.printStackTrace();
			log.error("获取低点警告时出错", e);
			return mapping.findForward(Constant.FAILURE);
		}
		
		return mapping.findForward(Constant.SUCCESS);
	}
	
	/**
	 * 获得当前会话的supervisor
	 * @return
	 */
	private DormSupervisor getCurrentDormSupervisor() {
		DormSupervisorWrapper dormSupervisorWrapper = (DormSupervisorWrapper) ContextUtil.getContext().getBean("dormSupervisorWrapper");
		DormSupervisor dormSupervisor = dormSupervisorWrapper.getDormSupervisor();
		return dormSupervisor;
	}
	
	private Document createNoticeXML(Boolean status, String message) {
		Document document = XMLUtil.createEncodingDocument("UTF-8");
		Element noticeElement = document.addElement("response").addElement("notice");
		noticeElement.addElement("status").addText(status.toString());
		noticeElement.addElement("message").addText(message);
		return document;
	}
	
	private JSONObject createJSONObject(Boolean status, String message) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		jsonMap.put("status", status);
		jsonMap.put("message", message);
		return JSONObject.fromObject(jsonMap);
	}
	
	@Override
	protected ActionForward unspecified(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		return null;
	}
	
}
